<template>
  <div class="login-container ">
    <div class="left">
      <div class="title-box">
        <img src="../../assets/img/login_icon.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="phone" class="table_item">
            <el-input
              prefix-icon="el-icon-user"
              v-model="ruleForm.phone"
              placeholder="请输入手机号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password" class="table_item">
            <el-input
              show-password
              prefix-icon="el-icon-lock"
              v-model="ruleForm.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="16"
              ><div class="grid-content bg-purple">
                <el-form-item prop="code">
                  <el-input
                    prefix-icon="el-icon-key"
                    v-model="ruleForm.code"
                    placeholder="请输入验证码"
                  ></el-input>
                </el-form-item></div
            ></el-col>
            <el-col :span="7"
              ><div class="grid-content bg-purple-light">
                <img :src="picUrl" alt="" @click="pic" /></div
            ></el-col>
          </el-row>
          <div class="input_checkbox">
            <el-form-item prop="checked">
              <el-checkbox v-model="ruleForm.checked"> </el-checkbox>
              我已阅读并同意<i>用户协议</i>和<i>隐私条款</i>
            </el-form-item>
          </div>
          <div class="btn">
            <el-form-item>
              <el-button type="primary" style="width:100%" @click="login"
                >登 录</el-button
              ></el-form-item
            >
          </div>
          <div class="btn">
            <el-form-item>
              <el-button
                type="primary"
                style="width:100%"
                @click="dialogVisible"
                >注 册</el-button
              >
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
    <div class="right">
      <img src="../../assets/img/login_bg.png" alt="" />
    </div>
    <messagebox ref="messagebox"></messagebox>
  </div>
</template>
<script>
import messagebox from '../login/messagebox.vue'
import { setToken } from '@/utils/token.js'
export default {
  components: { messagebox },

  data () {
    return {
      picUrl: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      ruleForm: {
        phone: '18520409113',
        password: '123456',
        code: '',
        checked: true
      },
      rules: {
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('手机号不能为空'))
                return
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                callback(new Error('手机号不合法'))
                return
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请正确输入密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度为6-11个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '长度为4个字符', trigger: 'blur' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    dialogVisible () {
      this.$refs.messagebox.dialogVisible = true
    },
    pic () {
      this.picUrl =
        process.env.VUE_APP_BASEURL +
        '/captcha?type=login&id=' +
        Math.random() * 999
    },
    login () {
      this.$refs.ruleForm.validate((valid, response) => {
        // console.log(valid, response)
        if (!valid) return

        this.$axios.post('/login', this.ruleForm).then(res => {
          // console.log(res)
          if (res.data.code === 200) {
            this.$message.success('登录成功')

            // localStorage.setItem('heimamm66-token', res.data.data.token)
            setToken(res.data.data.token)
            this.$router.push('/layout/welcome')
          } else {
            this.$message({
              message: res.data.message,
              type: 'erroe',
              duration: 1000
            })
            this.pic()
          }
        })
      })
    }
  }
}
</script>
<style lang="less">
* {
  padding: 0;
  margin: 0;
  list-style: none;
  text-decoration: none;
  box-sizing: border-box;
}
#app,
body,
html {
  height: 100%;
}
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
}
.login-container .left {
  width: 478px;
  height: 550px;
  background: #f5f5f5;
  padding: 48px;
}
.login-container .left .title-box {
  display: flex;
  align-items: center;
}
.login-container .left .title-box img {
  width: 22px;
  height: 17px;
  margin-right: 16px;
}
.login-container .left .title-box .title {
  font-size: 24px;
  margin-right: 14px;
}
.login-container .left .title-box .line {
  width: 1px;
  background-color: #c7c7c7;
  height: 27px;
  margin-right: 12px;
}
.login-container .left .title-box .sub-title {
  font-size: 22px;
}
.table_item {
  margin-top: 20px;
  // margin-bottom: 20px;
}
.bg-purple-light img {
  // width: 100%;

  width: 100%;
  height: 40px;
}
.bg-purple-light {
  margin-left: 10px;
}
.input_checkbox {
  margin-top: 30px;
}
.input_checkbox i {
  color: #66b1ff;
  font-style: normal;
}
.btn {
  margin-top: 30px;
}
</style>
